package com.risk.ctrl.config.spring.interceptor;

import com.google.common.base.Strings;
import com.risk.ctrl.credit.service.IRcpMerchantApiRelaService;
import com.risk.ctrl.entity.dto.RcpMerchantDTO;
import com.risk.ctrl.entity.property.ReqProperty;
import com.risk.ctrl.enums.ErrorCodeEnum;
import com.risk.ctrl.exceptions.BusinessException;
import com.risk.ctrl.utils.Md5Utils;
import com.risk.ctrl.utils.StringBulidUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@RequiredArgsConstructor
@Slf4j
public class LimitStreamInterceptor implements HandlerInterceptor {
    private final IRcpMerchantApiRelaService rcpMerchantApiRelaService;
    private final ReqProperty reqProperty;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String sign = request.getHeader("sign");
        String appKey = request.getHeader("appKey");
        String ctime = request.getHeader("ctime");
        if(Strings.isNullOrEmpty(appKey) || Strings.isNullOrEmpty(sign) || Strings.isNullOrEmpty(ctime)){
            throw new BusinessException(ErrorCodeEnum.PARAM_ERROR.getCode(),"sign||appKey||ctime必填");
        }
        String requestURI = request.getRequestURI();
        String contextPath = request.getContextPath();
        String url = requestURI.substring(contextPath.length());
        RcpMerchantDTO merchantDTO = RcpMerchantDTO.builder()
                .time(Strings.isNullOrEmpty(ctime) ? null : Long.valueOf(ctime))
                .appKey(appKey)
                .sign(sign)
                .reqProperty(reqProperty)
                .url(url)
                .build();
        rcpMerchantApiRelaService.validate(merchantDTO);
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        String requestURI = request.getRequestURI();
        String contextPath = request.getContextPath();
        String url = requestURI.substring(contextPath.length());
        String sign = request.getHeader("sign");
        String appKey = request.getHeader("appKey");
        String ctime = request.getHeader("ctime");
        RcpMerchantDTO merchantDTO = RcpMerchantDTO.builder()
                .time(Strings.isNullOrEmpty(ctime) ? null : Long.valueOf(ctime))
                .appKey(appKey)
                .sign(sign)
                .reqProperty(reqProperty)
                .url(url)
                .build();
        rcpMerchantApiRelaService.updateReqNum(merchantDTO);

    }
}
